<!-- 消息 -->
<template>
  <publicPage isNavbar>
    <view slot="navbar">
      <u-navbar :placeholder="true" bgColor="#2DBCBE">
        <view slot="left"></view>
        <view slot="center" class="disFlxAC colorF navbarCenter"> 消息 </view>
        <view slot="right"></view>
      </u-navbar>
    </view>

    <view class="messagePage" v-if="conversationList.length">
      <!-- <u-button type="primary" @click="removeConver">删除会话,调试用,后期删除</u-button> -->
      <view
        v-for="item in conversationList"
        :key="item.key"
        @tap="handleRoute(item.conversationID, item)"
      >
        <TUI-conversation-item
          :data-type="item.type"
          :conversation="item"
        ></TUI-conversation-item>
      </view>
    </view>
    <view
      class="defaultPage disFlxCC bodyBox"
      style="height: 100%"
      v-if="!isLogin || (!conversationList.length && listLoad)"
    >
      <image src="https://onelive.obs.cn-south-1.myhuaweicloud.com/uniapp/static/empty4.png" style="width:140rpx;height:140rpx;" />
      <view class="pt-8 pb-47 color9">暂无消息</view>
    </view>
  </publicPage>
</template>

<script>
import TUIConversationItem from "./conversationItem.vue";
import { caculateTimeago } from "./common.js";

export default {
  components: {
    TUIConversationItem
  },
  data() {
    return {
      onPull: false, // 是否已监听拉取消息列表,如未执行则使用vuex判断是否初始化完成
      listLoad:  false, // 加载是否完成
      conversationList: [],
      conID: ""
    };
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    uni.$on("isSDKReady", (value) => {
      // 登入后拉去会话列表
      this.onPull = true;
      this.getConversationList(1);
      uni.$TUIKit.on(
        uni.$TUIKitEvent.CONVERSATION_LIST_UPDATED,
        this.onConversationListUpdated
      );
    });
  },
  onShow() {
    // 是否已监听拉取消息列表,如上方未执行则使用vuex判断执行
    if (!this.onPull && this.$store.state.isSDKReady) {
      this.onPull = true;
      this.getConversationList(2);
      uni.$TUIKit.on(
        uni.$TUIKitEvent.CONVERSATION_LIST_UPDATED,
        this.onConversationListUpdated
      );
    }
    this.$store.dispatch("getTotalUnreadMessageCount"); // 获取未读消息
  },
  onShareAppMessage(){},
  onShareTimeline(){},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    uni.$TUIKit.off(uni.$TUIKitEvent.SDK_READY, this.onConversationListUpdated);
  },
  methods: {
    onConversationListUpdated(event) {
      console.log("会话列表更新", event.data);
      setTimeout(() => {
        let list = []
        event.data.forEach(v => {
          v.timeago = caculateTimeago(v.lastMessage.lastTime * 1000);
          v.key = v.conversationID + v.lastMessage.lastTime
          if(v.type === 'C2C' && v.conversationID !== "C2Cadministrator"){
            list.push(v)
          }
        });
        this.conversationList = list
      }, 500);
    },

    getConversationList(type) {
      uni.$TUIKit.getConversationList().then((imResponse) => {
        console.log(imResponse, "会话列表", type);
        let list = []
        imResponse.data.conversationList.forEach(v=>{
          v.timeago = caculateTimeago(v.lastMessage.lastTime * 1000);
          v.key = v.conversationID + v.lastMessage.lastTime
          if(v.type === 'C2C' && v.conversationID !== "C2Cadministrator"){
            list.push(v)
          }
        })
        this.conversationList = list
        this.listLoad = true
      }).catch (()=>{
        this.listLoad = true;
      })
    },

    handleRoute(id, item) {
      if (item.type === "GROUP") {
        uni.navigateTo({
          url: `/pagesB/TUI-Chat/chat?conversationID=${id}&groupID=${item.groupProfile.groupID}`,
        });
      } else {
        uni.navigateTo({ url: `/pagesB/TUI-Chat/chat?conversationID=${id}` });
      }
    },
    // 删除当前会话,调试用
    removeConver(){
      this.conversationList.forEach(v=>{
        uni.$TUIKit.deleteConversation(v.conversationID)
      })
    }
  },
};
</script>
<style lang="scss" scoped>
.messagePage {
  height: 100%;
  overflow-y: auto;
}
</style>
